package com.hengda.museumonline.ui;

import android.annotation.TargetApi;
import android.content.DialogInterface;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.hengda.museumonline.MyApplication;
import com.hengda.museumonline.R;
import com.hengda.museumonline.entity.BaseBean;
import com.hengda.museumonline.entity.UinfoBean;
import com.hengda.museumonline.http.CallServer;
import com.hengda.museumonline.utils.UinfoUtil;
import com.litesuits.common.assist.Base64;
import com.litesuits.common.utils.PackageUtil;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.RequestMethod;
import com.yolanda.nohttp.rest.OnResponseListener;
import com.yolanda.nohttp.rest.Request;
import com.yolanda.nohttp.rest.Response;

/**
 * @author：JFWU on 2016/10/26 09:36
 * @E-mail：jeffy12138@126.com
 */
public class AccountActivity extends BaseActivity implements View.OnClickListener {
    private android.widget.TextView userlogin;
    private android.widget.LinearLayout editpass;
    private android.widget.TextView useremail;
    private android.widget.LinearLayout editemail;
    private android.widget.TextView mobile;
    private android.widget.LinearLayout editmobile;
    private android.widget.TextView createtime;
    private LinearLayout close;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_account);
        this.close = (LinearLayout) findViewById(R.id.close);
        close.setOnClickListener(this);
        this.createtime = (TextView) findViewById(R.id.create_time);
        this.editmobile = (LinearLayout) findViewById(R.id.editmobile);
        editmobile.setOnClickListener(this);
        this.mobile = (TextView) findViewById(R.id.mobile);
        this.editemail = (LinearLayout) findViewById(R.id.editemail);
        editemail.setOnClickListener(this);
        this.useremail = (TextView) findViewById(R.id.user_email);
        this.editpass = (LinearLayout) findViewById(R.id.editpass);
        editpass.setOnClickListener(this);
        this.userlogin = (TextView) findViewById(R.id.user_login);
        getUinfo();

    }

    private void getUinfo() {
        UinfoUtil uinfoUtil = new UinfoUtil(AccountActivity.this, spUtil.getToken());
        uinfoUtil.setOnUinfoListener(new UinfoUtil.OnUinfoListener() {
            @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
            @Override
            public void onUinfo(UinfoBean uinfoBean) {
                if (1 == uinfoBean.getStatus()) {
                    userlogin.setText(uinfoBean.getData().getUser_login());
                    useremail.setText(uinfoBean.getData().getUser_email());
                    mobile.setText(uinfoBean.getData().getMobile());
                    createtime.setText(uinfoBean.getData().getCreate_time().substring(0, 10));
                }
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.editpass:
                View passView = View.inflate(AccountActivity.this, R.layout.editpass, null);
                final EditText oldpass = (EditText) passView.findViewById(R.id.oldpass);
                final EditText pass = (EditText) passView.findViewById(R.id.pass);
                final EditText repass = (EditText) passView.findViewById(R.id.repass);
                new AlertDialog.Builder(AccountActivity.this).setTitle("修改密码")
                        .setIcon(android.R.drawable.ic_dialog_info)
                        .setView(passView)
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
//                                editpass(oldpass.getText().toString(), pass.getText().toString(),
//                                        repass.getText().toString());
                            }
                        })
                        .setNegativeButton("取消", null)
                        .show();
                break;
            case R.id.editemail:
                View emailView = View.inflate(AccountActivity.this, R.layout.editemail, null);
                final EditText email = (EditText) emailView.findViewById(R.id.email);
                new AlertDialog.Builder(AccountActivity.this).setTitle("修改邮箱")
                        .setIcon(android.R.drawable.ic_dialog_info)
                        .setView(emailView)
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                //  editemail(email.getText().toString());
                            }
                        })
                        .setNegativeButton("取消", null)
                        .show();
                break;
            case R.id.editmobile:
                View mobileView = View.inflate(AccountActivity.this, R.layout.editmobile, null);
                final EditText mobile = (EditText) mobileView.findViewById(R.id.mobile);
                final EditText code = (EditText) mobileView.findViewById(R.id.code);
                Button btCode = (Button) mobileView.findViewById(R.id.bt_code);
                btCode.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        getCode(mobile.getText().toString());
                    }
                });
                new AlertDialog.Builder(AccountActivity.this).setTitle("修改手机")
                        .setIcon(android.R.drawable.ic_dialog_info)
                        .setView(mobileView)
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                // editmobile(mobile.getText().toString(), code.getText().toString());
                            }
                        })
                        .setNegativeButton("取消", null)
                        .show();
                break;
            case R.id.close:
                finish();
                break;
        }
    }

    private void editmobile(String mobile, String code) {
        Request request = NoHttp.createStringRequest(MyApplication.url + "profile/editmobile", RequestMethod.POST);
        request.add("p", "a");
        request.add("v", PackageUtil.getAppPackageInfo(AccountActivity.this).versionName);
        request.add("token", spUtil.getToken());
        request.add("mobile", mobile);
        request.add("code", code);
        CallServer.getRequestInstance().add(AccountActivity.this, 0, request, new OnResponseListener() {
            @Override
            public void onStart(int what) {
            }

            @Override
            public void onSucceed(int what, Response response) {
                System.out.println("response:" + response.get().toString());
                if (200 == response.getHeaders().getResponseCode()) {
                    Gson gson = new Gson();
                    BaseBean basebean = gson.fromJson(response.get().toString(), BaseBean.class);
                    if (1 == basebean.getStatus()) {
                        getUinfo();
                        getToast(basebean.getMsg());
                    } else {
                        getToast(basebean.getMsg());
                    }

                }

            }

            @Override
            public void onFailed(int what, String url, Object tag, Exception exception, int responseCode, long networkMillis) {
                System.out.println("onFailed:" + exception);
                getToast("网络错误");
            }

            @Override
            public void onFinish(int what) {
            }
        });
    }

    private void getCode(String mobile) {
        Request request = NoHttp.createStringRequest(MyApplication.url + "profile/send", RequestMethod.POST);
        request.add("p", "a");
        request.add("v", PackageUtil.getAppPackageInfo(AccountActivity.this).versionName);
        request.add("token", spUtil.getToken());
        request.add("mobile", mobile);
        CallServer.getRequestInstance().add(AccountActivity.this, 0, request, new OnResponseListener() {
            @Override
            public void onStart(int what) {
            }

            @Override
            public void onSucceed(int what, Response response) {
                System.out.println("验证码:" + response.get().toString());
            }

            @Override
            public void onFailed(int what, String url, Object tag, Exception exception, int responseCode, long networkMillis) {
                System.out.println("onFailed:" + exception);
                getToast("网络错误");
            }

            @Override
            public void onFinish(int what) {
            }
        });
    }

    private void editemail(String email) {
        Request request = NoHttp.createStringRequest(MyApplication.url + "profile/email", RequestMethod.POST);
        request.add("p", "a");
        request.add("v", PackageUtil.getAppPackageInfo(AccountActivity.this).versionName);
        request.add("token", spUtil.getToken());
        request.add("email", email);
        CallServer.getRequestInstance().add(AccountActivity.this, 0, request, new OnResponseListener() {
            @Override
            public void onStart(int what) {
            }

            @Override
            public void onSucceed(int what, Response response) {
                System.out.println("response:" + response.get().toString());
                if (200 == response.getHeaders().getResponseCode()) {
                    Gson gson = new Gson();
                    BaseBean basebean = gson.fromJson(response.get().toString(), BaseBean.class);
                    if (1 == basebean.getStatus()) {
                        getUinfo();
                        getToast(basebean.getMsg());
                    } else {
                        getToast(basebean.getMsg());
                    }

                }

            }

            @Override
            public void onFailed(int what, String url, Object tag, Exception exception, int responseCode, long networkMillis) {
                System.out.println("onFailed:" + exception);
                getToast("网络错误");
            }

            @Override
            public void onFinish(int what) {
            }
        });
    }

    private void editpass(String oldpass, String pass, String repass) {
        Request request = NoHttp.createStringRequest(MyApplication.url + "profile/editpass", RequestMethod.POST);
        request.add("p", "a");
        request.add("v", PackageUtil.getAppPackageInfo(AccountActivity.this).versionName);
        request.add("token", spUtil.getToken());
        request.add("oldpass", Base64.encodeToString(oldpass.getBytes(), Base64.DEFAULT));
        request.add("pass", Base64.encodeToString(pass.getBytes(), Base64.DEFAULT));
        request.add("repass", Base64.encodeToString(repass.getBytes(), Base64.DEFAULT));
        CallServer.getRequestInstance().add(AccountActivity.this, 0, request, new OnResponseListener() {
            @Override
            public void onStart(int what) {
            }

            @Override
            public void onSucceed(int what, Response response) {
                System.out.println("response:" + response.get().toString());
                if (200 == response.getHeaders().getResponseCode()) {
                    Gson gson = new Gson();
                    BaseBean basebean = gson.fromJson(response.get().toString(), BaseBean.class);
                    if (1 == basebean.getStatus()) {
                        getUinfo();
                        getToast(basebean.getMsg());
                    } else {
                        getToast(basebean.getMsg());
                    }

                }

            }

            @Override
            public void onFailed(int what, String url, Object tag, Exception exception, int responseCode, long networkMillis) {
                System.out.println("onFailed:" + exception);
                getToast("网络错误");
            }

            @Override
            public void onFinish(int what) {
            }
        });
    }
}
